<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage, ElDialog, ElInput, ElTable, ElTableColumn } from 'element-plus'
import { Edit, View, CopyDocument, Delete, Share, Plus, Download } from '@element-plus/icons-vue'
import { logger } from '../utils/logger'
import { wsManager } from '../utils/websocket'

// 获取当前时间
const currentTime = ref('')

// 更新时间的函数
const updateTime = () => {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  const seconds = String(now.getSeconds()).padStart(2, '0')
  currentTime.value = `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`
}

// 按钮状态
const buttonStates = ref({
  home: '正常',
  user: '正常',
  dataImport: '正常',
  dataManage: 'normal',
  dataAnnotation: 'normal'
})

// 鼠标事件处理
const handleMouseEnter = (button) => {
  buttonStates.value[button] = '悬浮'
}

const handleMouseLeave = (button) => {
  buttonStates.value[button] = '正常'
}

const handleMouseDown = (button) => {
  buttonStates.value[button] = '点击'
}

const handleMouseUp = (button) => {
  buttonStates.value[button] = '悬浮'
}

// 路由
const router = useRouter()

// 返回主页
const goToHome = () => {
  router.push('/')
}
// 模拟数据
const tableData = ref([
  {
    id: 1,
    name: '大狗检测',
    description: '优质的大狗',
    status: '未标注'
  },
  {
    id: 2,
    name: '卡皮巴拉检测',
    description: '过量的卡皮巴拉',
    status: '标注中'
  },
  {
    id: 3,
    name: '小狗检测',
    description: '114514',
    status: '已完成'
  }
])

// 处理各种操作按钮的点击事件
const handleEdit = (row) => {
  console.log('编辑', row)
}

const handleAnnotate = (row) => {
  console.log('标注', row)
}

const handleCopy = (row) => {
  console.log('复制', row)
}

const handleDelete = (row) => {
  console.log('删除', row)
}

const handleShare = (row) => {
  console.log('分享', row)
}

const showCreateDialog = ref(false)
const showDatasetDialog = ref(false)
const selectedDatasets = ref([])
const availableDatasets = ref([])
const taskForm = ref({
  name: '',
  description: '',
  images: []
})

const handleCreate = () => {
  showCreateDialog.value = true
}

const handleBack = () => {
  showCreateDialog.value = false
}

const handleRefresh = () => {
  // 刷新图片预览
  console.log('刷新图片预览')
}

const handleAddDataset = async () => {
  showDatasetDialog.value = true
  try {
    const response = await wsManager.sendMessage({
      operation: 'get_labeldata',
      params: [],
      authToken: 'string'
    })
    availableDatasets.value = response.data.params
  } catch (error) {
    console.error('获取数据集失败:', error)
  }
}

const handleMultiSelect = () => {
  console.log('多选功能')
}

const handleDatasetConfirm = async () => {
  showDatasetDialog.value = false
  try {
    const response = await wsManager.sendMessage({
      operation: 'newlabel_confirm',
      params: {
        name: taskForm.value.name,
        desc: taskForm.value.description,
        datalist: selectedDatasets.value
      },
      authToken: 'string'
    })
    tableData.value = response.data.params
  } catch (error) {
    console.error('创建任务失败:', error)
  }
}

const handleDatasetCancel = () => {
  showDatasetDialog.value = false
}

</script>

<template>
  <div class="data-management">
    <!-- 左侧边栏 -->
    <div class="sidebar">
      <div class="logo">
        <img src="/pic/dataManagement/logo-菜单左上方位置.png" alt="Logo">
      </div>
      <div class="nav-buttons">
        <div class="nav-button"
             @mouseenter="handleMouseEnter('dataManage')"
             @mouseleave="handleMouseLeave('dataManage')"
             @mousedown="handleMouseDown('dataManage')"
             @mouseup="handleMouseUp('dataManage')">
          <img :src="`/pic/dataManagement/数据管理-${buttonStates.dataManage === 'normal' ? '正常' : '悬浮'}.png`" alt="数据管理">
        </div>
        <div class="nav-button"
             @mouseenter="handleMouseEnter('dataAnnotation')"
             @mouseleave="handleMouseLeave('dataAnnotation')"
             @mousedown="handleMouseDown('dataAnnotation')"
             @mouseup="handleMouseUp('dataAnnotation')">
          <img :src="`/pic/dataManagement/数据标注-${buttonStates.dataAnnotation === 'normal' ? '正常' : '悬浮'}.png`" alt="数据标注">
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 顶部栏 -->
      <div class="top-bar">
        <div class="system-name">算筹平台系统</div>
        <div class="home-button"
             @mouseenter="handleMouseEnter('home')"
             @mouseleave="handleMouseLeave('home')"
             @mousedown="handleMouseDown('home')"
             @mouseup="handleMouseUp('home')"
             @click="goToHome">
          <img :src="`/pic/dataManagement/首页-${buttonStates.home}.png`" alt="首页">
        </div>
        <div class="current-time">{{ currentTime }}</div>
        <div class="user-info">
          <div class="user-button"
               @mouseenter="handleMouseEnter('user')"
               @mouseleave="handleMouseLeave('user')"
               @mousedown="handleMouseDown('user')"
               @mouseup="handleMouseUp('user')">
            <img :src="`/pic/dataManagement/用户-${buttonStates.user}.png`" alt="用户">
          </div>
          <span class="username">用户：XXXX</span>
        </div>
      </div>
      
      <!--  数据标注界面  -->
      <div class="annotation-content">
        <!-- 操作工具栏 -->
        <div class="action-toolbar">
          <button class="primary-button" @click="handleCreate">新建标注任务</button>
          <button class="success-button" @click="handleDownload">导出数据集</button>
        </div>

        <!-- 新建标注任务弹窗 -->
        <div class="create-dialog" v-if="showCreateDialog">
          <div class="dialog-header">
            <input v-model="taskForm.name" placeholder="请输入标注任务名称" />
          </div>
          <div class="dialog-body">
            <div class="image-preview">
              <!-- 图片预览区 -->
            </div>
            <div class="description-input">
              <textarea v-model="taskForm.description" placeholder="请输入任务描述"></textarea>
            </div>
          </div>
          <div class="dialog-footer">
            <button @click="handleBack">返回</button>
            <button @click="handleRefresh">刷新</button>
            <button @click="handleAddDataset">添加</button>
            <button @click="handleMultiSelect">多选</button>
          </div>
        </div>

        <!-- 数据集选择弹窗 -->
        <div class="dataset-dialog" v-if="showDatasetDialog">
          <div class="dialog-header">
            <h3>选择数据集</h3>
          </div>
          <div class="dialog-body">
            <ul>
              <li v-for="dataset in availableDatasets" :key="dataset.id">
                <input type="checkbox" v-model="selectedDatasets" :value="dataset.id" />
                {{ dataset.id }} - {{ dataset.size }}
              </li>
            </ul>
          </div>
          <div class="dialog-footer">
            <button @click="handleDatasetConfirm">确定</button>
            <button @click="handleDatasetCancel">取消</button>
          </div>
        </div>

        <!-- 任务列表表格 -->
        <table class="custom-table">
          <thead>
            <tr>
              <th>标注任务</th>
              <th>描述</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="row in tableData" :key="row.id">
              <td>{{ row.name }}</td>
              <td>{{ row.description }}</td>
              <td>{{ row.status }}</td>
              <td class="action-buttons">
                <button class="primary-button" @click="handleEdit(row)">编辑</button>
                <button class="success-button" @click="handleAnnotate(row)">标注</button>
                <button class="info-button" @click="handleCopy(row)">复制</button>
                <button class="danger-button" @click="handleDelete(row)">删除</button>
                <button class="warning-button" @click="handleShare(row)">分享</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<style scoped>

.action-toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.action-toolbar .el-button {
  cursor: pointer;
}

.custom-table {
  width: 100%;
  margin-top: 20px;
  border-collapse: collapse;
  font-size: 14px;
}

.custom-table th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: bold;
  padding: 12px 15px;
  border-bottom: 1px solid #ebeef5;
  text-align: left;
}

.custom-table td {
  padding: 12px 15px;
  border-bottom: 1px solid #ebeef5;
  vertical-align: middle;
}

.custom-table tr:hover td {
  background-color: #f5f7fa;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.action-buttons button {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 4px;
}

.primary-button {
  background-color: #409eff;
  color: white;
}

.success-button {
  background-color: #67c23a;
  color: white;
}

.info-button {
  background-color: #909399;
  color: white;
}

.danger-button {
  background-color: #f56c6c;
  color: white;
}

.warning-button {
  background-color: #e6a23c;
  color: white;
}

.el-table .el-button {
  margin-right: 5px;
  cursor: pointer;
}

.data-management {
  display: flex;
  height: 100vh;
  background-color: #f0f2f5;
}

.sidebar {
  width: 80px;
  background-color: #4a6baf;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 20px;
}

.logo img {
  width: 60px;
  height: auto;
}

.nav-buttons {
  margin-top: 20px;
}

.nav-button {
  margin-bottom: 10px;
  cursor: pointer;
}

.nav-button img {
  width: 60px;
  height: auto;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.top-bar {
  height: 60px;
  background-color: white;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.system-name {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.home-button {
  margin-left: 20px;
  cursor: pointer;
}

.current-time {
  margin-left: auto;
  color: #666;
}

.user-info {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.user-button {
  cursor: pointer;
  margin-right: 10px;
}

.username {
  color: #333;
}

.annotation-content {
  padding: 30px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.15);
  margin: 20px;
}
</style>